package com.hcrazy.manchester.app.controllers.ticket;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hcrazy.manchester.app.pojo.BookCustomer;
import com.hcrazy.manchester.app.pojo.BookOrder;
import com.hcrazy.manchester.app.pojo.ErrorMsg;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.pojo.User;
import com.hcrazy.manchester.app.services.BookFoodService;
import com.hcrazy.manchester.app.services.BookMenuService;
import com.hcrazy.manchester.app.services.BookOrderService;
import com.hcrazy.manchester.app.utils.Constant;
import com.hcrazy.manchester.app.utils.Log;

@Controller
@RequestMapping("/api")
public class TicketAPIController {
	
	@Autowired
	private BookMenuService menuService;
	
	@Autowired
	private BookFoodService foodService;
	
	@Autowired
	private BookOrderService orderService;

	@RequestMapping(value="/ticket/customer",method=RequestMethod.POST)
	public @ResponseBody Object updateUserInfo(
			@RequestBody BookCustomer customer,
			HttpServletRequest req){
		try {
			//获取当前登录用户
			Object obj = req.getSession().getAttribute("user");
			if(null == obj){
				return new ErrorMsg(Constant.MEMBER_LOGIN_NOT,"会员未登录！");
			}
			User user = (User)obj;
			customer.setUserId(user.getUserId());
			//sellerId=0标识为票务系统的客户
			customer.setSellerId(0);
			customer.setType(Constant.TYPE_BOOK_TICKET);
			
			orderService.addCustomer(customer);
			
			return setResult("true");
			
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	
	}
	
	private Map<String,String> setResult(String value){
		Map<String,String> result = new HashMap<String,String>();
		result.put(Constant.SINGLE_RETURN,value);
		return result;
	}
	
	/**
	 * 订票
	 * @return
	 */
	@RequestMapping(value="/ticket/order",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Object orderFood(
			HttpServletRequest req,@RequestBody BookOrder order){
		try {
			//如果用户未登录，返回错误码
			Object obj = req.getSession().getAttribute("user");
			if(null == obj){
				return new ErrorMsg(Constant.MEMBER_LOGIN_NOT,"会员未登录，不允许订票！");
			}
			
			User user = (User)obj;
			long userId = user.getUserId();
			
			order.setType(Constant.TYPE_BOOK_TICKET);
			order.setUserId(userId);
			order.setSellerId(0);
			
			orderService.addBookOrder(order);
			return setResult("true");
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	}
	
	@RequestMapping(value="/ticket/tickets",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Object getFoods(
			@RequestParam(required = false) Integer menuId,//所属票务分类
			@RequestParam(required = false) String search,//票务名称
			@RequestParam(required = false) Integer pageNum,//第几页
			@RequestParam(required = false) Integer numPerPage//一页请求多少条数据
			,HttpServletRequest req){
		PageModel pageModel = new PageModel();
		if(null != pageNum && pageNum > 0){
			pageModel.setPageNum(pageNum);
		}
		if(null != numPerPage && numPerPage > 0){
			pageModel.setNumPerPage(numPerPage);
		}
		
		if(null == menuId){
			menuId = 0;
		}
		if(null == search){
			search = "";
		}
		
		try {
			return foodService.getBookFoods(pageModel, 0, menuId, search, 0,Constant.TYPE_BOOK_TICKET);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	}
	
	@RequestMapping(value="/ticket/sorts",produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Object getAllBookMenus(){
		try {
			return menuService.getAllMenus(Constant.TYPE_BOOK_TICKET, 0);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	}
	
	@RequestMapping(value="/ticket/orders",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Object getOrders(
			@RequestParam(required = false) String search,//菜品名称
			@RequestParam(required = false) String orderDateStart,//菜品名称
			@RequestParam(required = false) String orderDateEnd,//菜品名称
			@RequestParam(required = false) Integer pageNum,//第几页
			@RequestParam(required = false) Integer status,//所属专业，可选
			@RequestParam(required = false) Integer numPerPage//一页请求多少条数据
			,HttpServletRequest req){
		
		PageModel pageModel = new PageModel();
		if(null != pageNum && pageNum > 0){
			pageModel.setPageNum(pageNum);
		}
		if(null != numPerPage && numPerPage > 0){
			pageModel.setNumPerPage(numPerPage);
		}
		
		if(null == search){
			search = "";
		}
		
		if(null == status){
			status = Constant.TYPE_BOOK_ORDER_ALL;
		}
		
		if(null == orderDateStart || null == orderDateEnd){
			orderDateStart = "";
			orderDateEnd = "";
		}
		
		Object object = req.getSession().getAttribute("user");
		if(null == object || !(object instanceof User)){
			return new ErrorMsg(Constant.MEMBER_LOGIN_NOT,"请首先登录系统");
		}
		User user = (User)object;
		
		try {
			return orderService.getPagedOrders(pageModel, search, user.getUserId(), 0, orderDateStart, orderDateEnd, status,Constant.TYPE_BOOK_TICKET);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	}
}
